package weaver.filter.login;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import com.api.login.bean.SessionBean;
import com.api.login.biz.UserOffline;
import org.dom4j.Element;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.commons.lang.StringUtils;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.*;
import weaver.hrm.resource.ResourceComInfo;
import weaver.security.core.SecurityCore;

import static org.springframework.web.util.WebUtils.getSessionAttribute;

/**
 * 需要添加到安全过滤器下面,web.xml  SecurityFilter
 * securityFilter放第一个，此过滤器放第二个
 *
 * @author zgy
 */
public class LoginExtensionFilter implements Filter {
    public static String[] loginUrls = null;
    public static String[] logoutUrls = null;
    public static boolean isAES = false;
    public static boolean enblechuanhao = false;
    private static SecurityCore sc = new SecurityCore();

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        /** 特输条件放行 */
        if (checkHead(req)) {
            new BaseBean().writeLog("特殊放行：",req.getRequestURI());
            chain.doFilter(request, response);
            return;
        }


        if (sc.checkSessionTimeout(req)) {
            new BaseBean().writeLog("--checkSessionTimeout为true，超时--");
            //	Util.setCookie(res, "extloginid", "", 0);
            LoginExtension.updateCookie("", res);
            //LoginExtension.clearCookie(req,res);
        } else {
            User userBean = (User) req.getSession().getAttribute("weaver_user@bean");
            if (userBean != null && userBean.getUID() != -1) {
                if ((req.getRequestURI().indexOf("/api") > -1) || req.getRequestURI().indexOf(".jsp") > -1) {
                    LoginExtension.dealChuanhao(userBean.getUID() + "", req, res);

                }
            } else {
                //处理超时
                LoginExtension.dealChaoshi(req, res);
            }
        }
        chain.doFilter(request, response);

    }

    public void init(FilterConfig filterConfig) {
        BaseBean bb = new BaseBean();
        if (GCONST.getRootPath() == null) {
            GCONST.setRootPath(filterConfig.getServletContext().getRealPath("/") + File.separatorChar);
        }
        bb.writeLog("##session log ## invoke LoginExtensionFilter.init() method...");
        String loginUrl = bb.getPropValue("weaver_login_extension", "loginUrl");
        String logoutUrl = bb.getPropValue("weaver_login_extension", "logoutUrl");
        String isAESstr = bb.getPropValue("weaver_login_extension", "isAES");

        if (StringUtils.isNotEmpty(isAESstr)) {
            isAES = Boolean.valueOf(isAESstr);
        }

        String str4 = bb.getPropValue("weaver_login_extension", "enblechuanhao");
        if (StringUtils.isNotEmpty(str4)) {
            enblechuanhao = Boolean.valueOf(str4);
        }

        loginUrls = loginUrl.split(",");
        logoutUrls = logoutUrl.split(",");
    }

    public void destroy() {
        BaseBean bb = new BaseBean();
        int i = 1;

        while (true) {
            bb.writeLog("##############  : " + i);
            i++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (i > 100) break;
        }
    }

    private boolean checkHead(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        if (requestURI.contains("GetCarDatas")||requestURI.contains("/apis/QR")){
            return true;
        }else if (request.getHeader("token")!=null||request.getHeader("appid")!=null){
            return true;
        }
        return false;
    }


}
